import {AzraelFrameWorkComponent} from "../Base/AzraelFrameWork";
import GameFrameworkEntry from "../../src/Base/GameFrameworkEntry";
import {IProcedureManager} from "../../src/Procedure/IProcedureManager";
import {ProcedureBase} from "../../src/Procedure/ProcedureBase";
import GameFrameworkModule from "../../src/Base/GameFrameworkModule";
import ProcedureManager from "../../src/Procedure/ProcedureManager";
import FsmManager from "../../src/Fsm/FsmManager";

const {ccclass, property} = cc._decorator;

@ccclass
export class ProcedureComponent extends AzraelFrameWorkComponent {
    @property([cc.String])
    public StateTypeNames: string[] = [];
    @property(cc.String)
    public CurrentStateName: string = "";

    private _ProcedureManager: IProcedureManager & GameFrameworkModule = null;
    private _EntranceProcedure: ProcedureBase = null;

    protected onLoad(): void {
        super.onLoad();
        this._ProcedureManager = GameFrameworkEntry.GetModule(ProcedureManager);
    }

    protected start(): void {
        let arr = [];
        let entertype = undefined;
        for (let i = 0; i < this.StateTypeNames.length; i++) {
            let proceduretype =  <any>cc.js.getClassByName(this.StateTypeNames[i]);
            if (proceduretype) {
                let procedure = new proceduretype();
                if (this.CurrentStateName == this.StateTypeNames[i] && this.CurrentStateName != "") {
                    entertype = proceduretype;
                    this._EntranceProcedure = procedure as ProcedureBase;
                }
                arr.push(procedure);
            }
        }
        this._ProcedureManager.Initialize(GameFrameworkEntry.GetModule(FsmManager),arr);
        this._ProcedureManager.StartProcedure(entertype.prototype);

    }


    /// <summary>
    /// 获取当前流程。
    /// </summary>
    public get CurrentProcedure(): ProcedureBase {
        return this._ProcedureManager.CurrentProcedure();
    }


/// <summary>
/// 获取当前流程持续时间。
/// </summary>
    public get CurrentProcedureTime(): number {
        return this._ProcedureManager.CurrentProcedureTime();
    }

    HasProcedure<T extends ProcedureBase>(procedureType: { prototype: T }): boolean {
        return this._ProcedureManager.HasProcedure(procedureType);
    }

    GetProcedure<T extends ProcedureBase>(procedureType: { prototype: T }): T {
        return this._ProcedureManager.GetProcedure(procedureType) as T;
    }


}